package com.tarena.luban.worker.server.infrastructure.repo.impl;

import com.tarena.luban.worker.server.dao.api.WorkerAreaDAO;
import com.tarena.luban.worker.server.dao.api.WorkerDAO;
import com.tarena.luban.worker.server.domain.bo.WorkerAreaBO;
import com.tarena.luban.worker.server.domain.repository.WorkerAreaRepository;
import com.tarena.luban.worker.server.infrastructure.converter.WorkerAreaConverter;
import com.tarena.tp.luban.worker.po.WorkerArea;
import com.tarena.tp.luban.worker.server.dto.param.WorkerAreaParam;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class WorkerAreaRepoImpl implements WorkerAreaRepository {
    @Autowired
    private WorkerAreaDAO workerAreaDAO;
    @Autowired
    private WorkerAreaConverter workerAreaConverter;
    @Override
    public void delete(Long userId) {
        workerAreaDAO.delete(userId);
    }

    @Override
    public void saveArea(WorkerCreateParam workerCreateParam) {
        List<WorkerArea> workerAreas=new ArrayList<>();
        List<WorkerAreaParam> workerAreaParams = workerCreateParam.getWorkerAreaParams();
        for (WorkerAreaParam workerAreaParam : workerAreaParams) {
            WorkerArea workerArea = workerAreaConverter.param2po(workerAreaParam);
            workerArea.setUserId(workerCreateParam.getUserId());
            workerAreas.add(workerArea);
        }
        workerAreaDAO.insertAreas(workerAreas);
    }

    @Override
    public List<WorkerAreaBO> getAreas(Long userId) {
        List<WorkerArea> areas=workerAreaDAO.selectAreas(userId);
        return workerAreaConverter.poList2BoList(areas);
    }
}
